/**
 * Interface: Utils
 *
 * Purpose: General utilities
 *
 * @author Ken Scott
 */

package org.openfantasia.util;

import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class Utils extends DateTime {

	public static void beep() {
		Toolkit.getDefaultToolkit().beep();
	}


	public static List<?> removeDuplicates(List<?> objects) {
		List result = new ArrayList();
		for (Object obj : objects) {
			if (!result.contains(obj)) {
				result.add(obj);
			}
		}
		return (List<?>) result;
	}


	public static List<String> removeCommentLines(List<String> strs) {
		List<String> result = new ArrayList<String>();
		for (String str : strs) {
			String s = str.trim();
			if (!s.startsWith("//") && !s.startsWith("#") && !((s.startsWith("/*") && s.endsWith("*/")))) {
				result.add(str);
			}
		}
		return result;
	}


	public static List<String> removeCommentBlocks(List<String> strs) {
		boolean blocking = false;
		List<String> result = new ArrayList<String>();
		for (String str : strs) {
			String s = str.trim();
			if (!blocking && s.contains("/*")) {
				blocking = true;
				s = s.substring(0, s.indexOf("/*"));
			}
			if (blocking && s.contains("*/")) {
				blocking = false;
				s = s.substring(s.indexOf("*/") + "*/".length());
			}
			s = s.trim();
			if (!blocking && TextUtils.hasText(s)) {
				result.add(s);
			}
		}
		return result;
	}


	public static String removeComments(String str) {
		String result = str;
		String before;
		do {
			before = result;
			result = TextUtils.removeBetween(result, "/*", "*/", true, true);
		} while (!before.equals(result));

//		result = TextUtils.removeAfter(result, "//", true);
		return result;
	}


	public static List removeNulls(List objs) {
		if (objs != null) {
			List<Object> result = new ArrayList<Object>();
			for (Object obj : objs) {
				if (obj != null) {
					result.add(obj);
				}
			}
			return result;
		}
		else {
			return null;
		}
	}


	public static List<String> removeEmptyLines(List<String> strs) {
		List<String> result = new ArrayList<String>();
		for (String str : strs) {
			String s = str.trim();
			if (str.trim().length() > 0) {
				result.add(str);
			}
		}
		return result;
	}


	/**
	 * Display a prompt string, and return whatever the user types up to the CR
	 *
	 * @param q
	 * @return
	 */
	public static String cliQuestion(String q) {
		System.out.print(q + "? ");

		StringBuilder sb = new StringBuilder();
		try {
			char ch = (char) System.in.read();
			while ((ch != -1) && (ch != '\r') && (ch != '\n')) {
				sb.append(ch);
				ch = (char) System.in.read();
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString().trim();
	}


	/**
	 * Display a prompt string, and return the first character the user types in
	 *
	 * @param q
	 * @return
	 */
	public static String cliPrompt(String q) {
		System.out.print(q + "? ");

		StringBuilder sb = new StringBuilder();
		try {
			sb.append((char) System.in.read());
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString().trim();
	}


	/**
	 * Launch the default browser, displaying the given URL
	 *
	 * @param url
	 */
	public static void launchBrowser(String url) {
		if (Desktop.isDesktopSupported()) {
			Desktop desktop = Desktop.getDesktop();
			if (desktop.isSupported(Desktop.Action.BROWSE)) {
				try {
					desktop.browse(new URI(url));
				}
				catch (IOException ioe) {
					ioe.printStackTrace();
				}
				catch (URISyntaxException use) {
					use.printStackTrace();
				}
			}
		}
	}


	public static String getLocalMacAddress() {
		String result = "INVALID";
		try {
			/* Get NetworkInterface for the current host and then read the hardware address. */
			NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
			byte[] mac = ni.getHardwareAddress();

			/* Extract each array of mac address and convert it to hex with the following format 08-00-27-DC-4A-9E. */
			StringWriter sb = new StringWriter();
			PrintWriter pw = new PrintWriter(sb);
			for (int i = 0; i < mac.length; i++) {
				pw.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
			}
			result = sb.getBuffer().toString();
		}
		catch (UnknownHostException e) {
			e.printStackTrace();
		}
		catch (SocketException e) {
			e.printStackTrace();
		}
		return result;
	}
}
